package com.hp.easyBuy.controller;
/*
* 留言控制器
* 时间：2024年4月1日 19:42:41
* 作者:代奥
* */

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.easyBuy.contants.RedixKey;
import com.hp.easyBuy.contants.StatusCode;
import com.hp.easyBuy.entity.DTO.TCommentDTO;
import com.hp.easyBuy.entity.DTO.TCommentDTO2;
import com.hp.easyBuy.entity.TComment;
import com.hp.easyBuy.entity.TUser;
import com.hp.easyBuy.service.CommentService;
import com.hp.easyBuy.utils.R;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("comment")
public class CommentController {
    @Resource
    private CommentService commentService;
    @Resource
    private RedisTemplate redisTemplate;




    //获取用户的分页的留言数据
    //currentpage和pagesize是分页条件
    @RequestMapping("userPage")
    public R doPage(Integer currentpage, Integer pagesize,HttpServletRequest req){
        Page page = new Page(currentpage,pagesize);
        //从req里面获取对应的token，然后从redis里面获取对应的user信息
        String token = req.getHeader("token");
       //从redis获取对应的用户信息
        TUser user = (TUser)redisTemplate.opsForValue().get(RedixKey.LOGIN_USER+token);
        System.out.println("user = " + user);
        Page<TCommentDTO> tCommentPage = commentService.doPage(currentpage, pagesize,user.getUserid());
        return R.success(tCommentPage);
    }

    //获取用户的分页的留言数据
    //currentpage和pagesize是分页条件
    @RequestMapping("allPage")
    public R doAllPage(Integer currentpage, Integer pagesize){
        Page page = new Page(currentpage,pagesize);
        Page<TCommentDTO> tCommentPage = commentService.doPage(currentpage, pagesize,null);
        return R.success(tCommentPage);
    }

    //个人留言框，添加个人留言框信息
    @RequestMapping("addComment")
    public R doAdd(@RequestBody TComment comment, HttpServletRequest req){
        //从req里面获取对应的token，然后从redis里面获取对应的user信息
        String token = req.getHeader("token");
        //从redis获取对应的用户信息
        TUser user = (TUser)redisTemplate.opsForValue().get(RedixKey.LOGIN_USER+token);
        comment.setUserid(user.getUserid());
        if(commentService.addComment(comment)){
            return R.success(StatusCode.SUCCESS, "新增留言成功");
        }else{
            return R.error(StatusCode.ERROR1, "新增留言失败");
        }
    }

    //获取单个留言信息
    @RequestMapping("getCommentById")
    public R getCommentById(Integer commentid,HttpServletRequest req){
        //从req里面获取对应的token，然后从redis里面获取对应的user信息
        String token = req.getHeader("token");
        //从redis获取对应的用户信息
        TUser user = (TUser)redisTemplate.opsForValue().get(RedixKey.LOGIN_USER+token);
        TCommentDTO2 commentDTO2Byid = commentService.getCommentDTO2Byid(commentid, user.getUserid());
        if(commentDTO2Byid == null){
            //证明留言对象和实际操作不是同一个用户
            return R.error();
        }else {
            return R.success(commentDTO2Byid);
        }
    }

    //更新留言
    @RequestMapping("updateComment")
    public R updateComment(@RequestBody TComment tComment,HttpServletRequest req){
        //获取操作id
        //从req里面获取对应的token，然后从redis里面获取对应的user信息
        String token = req.getHeader("token");
        //从redis获取对应的用户信息
        TUser user = (TUser)redisTemplate.opsForValue().get(RedixKey.LOGIN_USER+token);
        if(user.getUserid() == tComment.getUserid()){
            //两者相等那么就可以实现更新功能
            commentService.updateById(tComment);
            return R.success();
        }else{
            return R.error(StatusCode.ERROR1,"您不是留言当前对象,无法更新该留言");
        }
    }

    //删除用户
    @RequestMapping("del")
    public R doDel(Long commentid,HttpServletRequest req){
        //获取操作id
        //从req里面获取对应的token，然后从redis里面获取对应的user信息
        String token = req.getHeader("token");
        //从redis获取对应的用户信息
        TUser user = (TUser)redisTemplate.opsForValue().get(RedixKey.LOGIN_USER+token);
        TComment comment = commentService.getById(commentid);
        if(comment == null){
            return R.error(StatusCode.ERROR1, "该留言已被删除");
        }else if(comment.getStatus() == 1){
            return R.error(StatusCode.ERROR2, "该留言未被回复无法删除");
        }else if(comment.getUserid() != user.getUserid()){
            return R.error(StatusCode.ERROR3, "只能删除登录用户的留言");
        }else if(commentService.removeById(commentid)){
            return R.success(StatusCode.SUCCESS, "删除留言成功");
        }else{
            return R.error(StatusCode.ERROR4, "删除留言失败");
        }
    }




}
